package com.hbnu.controller;

import com.hbnu.req.UserLoginReq;
import com.hbnu.req.UserSaveReq;
import com.hbnu.resp.CommonResp;
import com.hbnu.resp.UserLoginResp;
import com.hbnu.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: hjx
 * @Date: 2023/6/14 08:04
 * @Description:
 */
@RestController
@CrossOrigin        //解决跨域问题
@RequestMapping("/sys-user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @PostMapping("/register")
    public CommonResp register(@RequestBody UserSaveReq req) {

        //前端传过来的值后端进行加密存储到数据库中
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        sysUserService.register(req);
        return resp;
    }

    @PostMapping("/login")
    public CommonResp login(@RequestBody UserLoginReq req) {
        //登录时候要拿前端传入的值进行到后端加密的数据进行比对
        req.setPassword(DigestUtils.md5DigestAsHex(req.getPassword().getBytes()));
        CommonResp resp = new CommonResp<>();
        UserLoginResp userLoginResp = sysUserService.login(req);
        resp.setContent(userLoginResp);
        return resp;
    }
}

